<?php
class CommentsController extends AppController{
	public $helper = array('Html','Form');
	
	function isAuthorized($user){
		if ($this->action=='savecomment'){
			return TRUE;
		}
		return parent::isAuthorized($user);
	}
//	public function beforeFilter(){
//		
//	}
	public function comment(){
		$this->set('comments',$this->Comment->find('all'));
	}
	
	public function getCommentByPostId($post_id){
		$comments = $this->Comment->query("Select users.username,comments.content,comments.create_time from comments,users where users.id=comments.user_id and post_id=$post_id order by create_time asc");
		return $comments;
	}
	
    public function savecomment(){

		    if ($this->request->is('post')) {
		       if ($this->request->data['Comment']['content']) {
		         $this->request->data['Comment']['user_id'] = $this->Auth->user('id');
                 date_default_timezone_set('Asia/Ho_Chi_Minh');
                 $datetime = date('Y/m/d H:i:s ', time());
			     $this->request->data['Comment']['create_time'] = $datetime;
			    
		         if ($this->Comment->save($this->request->data)){
		         	$this->redirect(array('controller'=>'Posts','action'=>'view', $this->request->data['Comment']['post_id']));
		         }
			     else{
			     	$this->Session->setFlash('Unable to save your comment');
			     }
		       }

		    }
    }

}